//
//  GMSmUtils.m
//  iosTestDemo
//
//  Created by 证联 on 2022/7/1.
//

#import "GMSmUtils.h"
#import "GMSm2Utils.h"


@implementation GMSmUtils

+ (NSString *)sm2Encrypt:(NSString *)key publicKey:(NSString *)pubKey{
    //key 公钥加密
    NSString *text= [GMSm2Utils encryptText:key publicKey:pubKey];
    //解码
    NSString *C1C3C2= [GMSm2Utils asn1DecodeToC1C3C2:text];
    //将C1C3C2 转换 C1C2C3
    NSString *C1C2C3= [GMSm2Utils convertC1C3C2ToC1C2C3:C1C3C2 hasPrefix:NO];
    //C1C2C3密文转换小写
    NSString *sm4Key= [C1C2C3 lowercaseString];
    return sm4Key;
}
+(nullable NSString *)sm2Decrypt:(NSString *)sm4Key privateKey:(NSString *) prikey{
    //去掉04
    NSString *string2 = [sm4Key substringFromIndex:2];
     //   C1C2C3转换C1C3C2
       NSString *C1C3C2 =[ GMSm2Utils convertC1C2C3ToC1C3C2:string2 hasPrefix:NO];
       //编码
       NSString *asn = [GMSm2Utils asn1EncodeWithC1C3C2:C1C3C2];
 //        NSLog(@"解密sm2- C1C2C3转换C1C3C2===C1C3C2=%@",asn);
       //解密
    NSString *deSM4Key = [GMSm2Utils decryptToText:asn  privateKey:prikey];
    return deSM4Key;
}

+ (NSString *)sm2Sign:(NSDictionary *)dic privateKey:(NSString *)prikey key:(NSString *)key{
    
    NSString *jsonVerify = [self stingToJson:dic];
    
    NSString * jsonString =[jsonVerify stringByAppendingString: key];
    //加签
    NSString *sign =[GMSm2Utils signText:jsonString privateKey:prikey userID:nil];
    //der编码
    NSString *derData =[GMSm2Utils derEncode:sign];
    //大写转小写
    NSString *checkValue =[derData lowercaseString];
    return checkValue;
}
+ (BOOL)sm2Verify:(NSDictionary *)jsonData publicKey:(NSString *)pubKey key:(NSString *) key{
    
    NSMutableDictionary *mutDictJSON = [NSMutableDictionary dictionary];
    mutDictJSON[@"encMsg"]=[jsonData objectForKey:@"encMsg"];
    mutDictJSON[@"instuId"]=[jsonData objectForKey:@"instuId"];
    mutDictJSON[@"interfaceId"]=[jsonData objectForKey:@"interfaceId"];
    mutDictJSON[@"reqSeqId"]=[jsonData objectForKey:@"reqSeqId"];
    mutDictJSON[@"sm4Key"]=[jsonData objectForKey:@"sm4Key"];
    mutDictJSON[@"timeStamp"] =[jsonData objectForKey:@"timeStamp"];
    mutDictJSON[@"verNum"] =[jsonData objectForKey:@"verNum"];
    //转换Sting
    NSString *jsonVerify = [self stingToJson:mutDictJSON];
    //验签{}+sm4Key
    jsonVerify = [jsonVerify stringByAppendingString:key];
    NSString *checkValueJSON =[jsonData objectForKey:@"checkValue"];
//    NSLog(@" 后端 验签  checkValue==%@",checkValueJSON);
    //签名进行DER解码
    NSString * derCheckValue = [GMSm2Utils derDecode:checkValueJSON];
    //验证签名
    BOOL isOK12 = [GMSm2Utils verifyText:jsonVerify signRS:derCheckValue publicKey:pubKey userID:nil];
    if(isOK12){
        return YES;
       
    }else{
        return NO;
        
    }
}

+ (nullable NSString *)stingToJson:(NSDictionary *)dic
{
    NSArray *key=@[@"encMsg",@"instuId",@"interfaceId",@"reqSeqId",@"sm4Key",@"timeStamp",@"verNum"];
    NSString *jsonStr=@"{";
    
    for (int i=0; i<key.count; i++) {
        NSString *jsonKey = [ NSString stringWithFormat:@"%@%@%@",@"\"" , key[i],@"\""];
        NSString *jsonValue = [ NSString stringWithFormat:@"%@%@%@",@"\"" , [dic objectForKey:key[i]],@"\""];
        
        if (i == key.count -1 ) {
            jsonStr =[NSString stringWithFormat:@"%@%@:%@}",jsonStr,jsonKey,jsonValue];
        }else{
            jsonStr =[NSString stringWithFormat:@"%@%@:%@,",jsonStr,jsonKey,jsonValue];
        }
        
    }
//    NSLog(@"json=%@",jsonStr);
    return jsonStr;
}
@end
